<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRoute } from 'vue-router'
import { timeFormat, toYuan, timeFormat_2 } from '@/hooks/util'
import * as ocrApi from '@/api/ocr'
import { message } from 'ant-design-vue'
import Tip from './components/Tip.vue'

const route = useRoute()
const tifRef = ref()

interface Pagination {
  page: number
  pageSize: number
  total: number
  current?: number
  onChange: Function
  showSizeChanger: boolean
  showQuickJumper: boolean
}

const handlePageChange = (page: any, pageSize: any) => {
  // console.log(page, pageSize)
  pagination.value.pageSize = pageSize
  searchParams.value.pageSize = pageSize
}

const pagination = ref<Pagination>({
  page: 1,
  pageSize: 10,
  total: 0,
  onChange: handlePageChange,
  showSizeChanger: true,
  showQuickJumper: true,
})
let loading = ref<boolean>(false)

interface SearchParams {
  currentPage: number
  pageSize: number
  [propName: string]: any
}
const searchParams = ref<SearchParams>({
  currentPage: pagination.value.page,
  pageSize: pagination.value.pageSize,
})

const dataSource = ref([])
const columns = [
  {
    title: '识别图片',
    key: 'imgUrl',
    width: 100,
    algin: 'center',
  },
  {
    title: '电影信息',
    key: 'filmName',
    width: 300,
  },
  {
    title: '识别结果',
    key: 'userName',
    width: 300,
  },
  {
    title: '影院信息',
    key: 'cinemaName',
    width: 400,
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
    fixed: 'right',
  },
]

// 翻页
const changePage = (e: any) => {
  pagination.value.current = e.current
  pagination.value.pageSize = e.pageSize
  searchParams.value.currentPage = pagination.value.current as number
  getList()
}

const getList = async () => {
  loading.value = true
  try {
    let { state, data, message: msg } = await ocrApi.filmIdentifyList(searchParams.value)
    if (state === 200) {
      dataSource.value = data.list
      pagination.value.total = Number(data.totalCount)
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
  loading.value = false
}

// 翻译渠道
const getChannel = (num: string | number) => {
  if (num == 1) {
    return '微信'
  } else if (num == 2) {
    return '企业微信'
  } else if (num == 3) {
    return '闲鱼'
  }
}

onMounted(() => {
  searchParams.value.channel = '3'
  getList()
})

import type { SelectProps } from 'ant-design-vue'
const identifyStateList = ref<SelectProps['options']>([
  {
    value: -1,
    label: '识别失败',
  },
  {
    label: '识别中',
    value: 0,
  },
  {
    label: '识别成功',
    value: 1,
  },
])
const channelList = ref<SelectProps['options']>([
  {
    label: '微信',
    value: '1',
  },
  // {
  //   label: '企业微信',
  //   value: '2',
  // },
  {
    label: '闲鱼',
    value: '3',
  },
])

const formRef = ref()
interface FormState {
  cinemaName?: string
  fansId?: string
  identifyState?: string
  channel?: string
  [propName: string]: any
}
const formState = reactive<FormState>({
  channel: '3',
})

// 查询
const search = () => {
  pagination.value.current = 1
  searchParams.value.currentPage = 1
  formRef.value.validate().then(() => {
    searchParams.value.cinemaName = formState.cinemaName || ''
    searchParams.value.fansId = formState.fansId || ''

    if (
      formState.identifyState == '0' ||
      formState.identifyState == '-1' ||
      formState.identifyState == '1'
    ) {
      searchParams.value.identifyState = formState.identifyState
    }

    searchParams.value.channel = formState.channel || ''
    getList()
  })
}

// 重置
const reset = () => {
  pagination.value.current = 1
  searchParams.value.currentPage = 1
  formRef.value.resetFields()

  formRef.value.resetFields()
  for (let key in searchParams.value) {
    if (key != 'currentPage' && key != 'pageSize' && key != 'robotId')
      searchParams.value.hasOwnProperty(key) && delete searchParams.value[key]
  }

  searchParams.value.channel = '3'
  getList()
}

const showTip = (id: string) => {
  tifRef.value.showModal(id)
}
</script>

<template>
  <page-container :title="route.meta.title">
    <a-card style="overflow-x: scroll">
      <div class="searchBox">
        <a-form ref="formRef" layout="inline" :model="formState" class="formClass">
          <a-form-item label="影院名称" name="cinemaName">
            <a-input
              v-model:value="formState.cinemaName"
              placeholder="影院名称"
              style="width: 200px"
            />
          </a-form-item>

          <a-form-item label="粉丝ID" name="fansId">
            <a-input v-model:value="formState.fansId" placeholder="粉丝ID" style="width: 200px" />
          </a-form-item>

          <a-form-item label="识别状态" name="identifyState">
            <a-select
              v-model:value="formState.identifyState"
              placeholder="识别状态"
              style="width: 200px"
            >
              <a-select-option
                v-for="(item, index) in identifyStateList"
                :key="index"
                :value="item.value"
              >
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>

          <a-form-item label="渠道" name="channel">
            <a-select v-model:value="formState.channel" placeholder="渠道" style="width: 200px">
              <a-select-option
                v-for="(item, index) in channelList"
                :key="index"
                :value="item.value"
              >
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>

          <a-form-item>
            <a-button type="primary" @click="search">搜索</a-button>
            <a-button @click="reset">重置</a-button>
          </a-form-item>
        </a-form>
      </div>

      <a-alert
        description="小提示：只要鼠标在表格上，按住左Shift键并滚动鼠标滚轮就可以横向滚动表格"
        type="info"
        banner
        style="margin-bottom: 10px; padding: 10px"
      />

      <a-table
        style="overflow-x: scroll"
        :columns="columns"
        :data-source="dataSource"
        :pagination="pagination"
        :loading="loading"
        @change="changePage"
        rowKey="id"
        size="small"
        :scroll="{ x: 100 }"
      >
        <template #emptyText>
          <a-empty description="暂无数据" />
        </template>

        <template #headerCell="{ column }">
          <template v-if="column.key === 'id'">
            <span>id</span>
          </template>
          <template v-else-if="column.key === 'imgUrl'">
            <div style="text-align: center">识别图片</div>
          </template>
        </template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'imgUrl'">
            <a-popover placement="rightTop">
              <template #content>
                <div class="popoverImg">
                  <img :src="record.imgUrl" alt="" />
                </div>
              </template>
              <div class="imgBox">
                <img :src="record.imgUrl" alt="获取失败..." />
              </div>
            </a-popover>
          </template>

          <template v-if="column.key === 'filmName'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="电影名称">
                <a-tooltip placement="top">
                  <template #title>
                    <span>{{ record.filmName || '未识别' }}</span>
                  </template>
                  <a-tag color="blue" class="hiddenText">{{ record.filmName || '未识别' }}</a-tag>
                </a-tooltip>
              </a-descriptions-item>
              <a-descriptions-item label="影片类型">
                {{ record.planType || '无' }}
              </a-descriptions-item>
              <a-descriptions-item label="放映时间">
                {{ timeFormat_2(record.showTime) }}
              </a-descriptions-item>
              <a-descriptions-item label="提交时间">
                {{ timeFormat_2(record.submitTime) }}
              </a-descriptions-item>
            </a-descriptions>
          </template>

          <template v-if="column.key === 'cinemaName'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="城市">
                {{ record.city || '-' }}
              </a-descriptions-item>
              <a-descriptions-item label="影院名称">
                <a-tooltip placement="top">
                  <template #title>
                    <span>{{ record.cinemaName || '未识别' }}</span>
                  </template>
                  <span class="hiddenText">{{ record.cinemaName || '未识别' }}</span>
                </a-tooltip>
              </a-descriptions-item>
              <a-descriptions-item label="影院地址">
                <a-tooltip placement="top">
                  <template #title>
                    <span>{{ record.cinemaAddress || '未识别' }}</span>
                  </template>
                  <span class="hiddenText">{{ record.cinemaAddress || '未识别' }}</span>
                </a-tooltip>
              </a-descriptions-item>
              <a-descriptions-item label="影厅">
                <a-tooltip placement="top">
                  <template #title>
                    <span>{{ record.showName || '未识别' }}</span>
                  </template>
                  <span class="hiddenText">{{ record.showName || '未识别' }}</span>
                </a-tooltip>
              </a-descriptions-item>
            </a-descriptions>
          </template>

          <template v-else-if="column.key === 'userName'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="用户名">
                {{ record.userName }}
              </a-descriptions-item>
              <a-descriptions-item label="座位">
                <a-tooltip placement="top">
                  <template #title>
                    <div class="hiddenText" v-for="item in record.seatNos.split(',')">
                      <a-tag color="blue">{{ item }}</a-tag>
                    </div>
                  </template>
                  <div class="hiddenText" v-for="item in record.seatNos.split(',')">
                    <a-tag color="blue">{{ item }}</a-tag>
                  </div>
                </a-tooltip>
              </a-descriptions-item>
              <a-descriptions-item label="识别状态">
                <a-tag color="red" v-if="record.identifyState == -1">失败</a-tag>
                <a-tag color="blue" v-if="record.identifyState == 0">识别中</a-tag>
                <a-tag color="green" v-if="record.identifyState == 1">成功</a-tag>
              </a-descriptions-item>
              <a-descriptions-item label="影片海报">
                <a-popover placement="right">
                  <template #content>
                    <div class="popoverImg">
                      <img :src="record.filmImg" alt="" />
                    </div>
                  </template>
                  <a>查看影片海报</a>
                </a-popover>
              </a-descriptions-item>
            </a-descriptions>
          </template>

          <template v-else-if="column.key === 'action'">
            <div style="width: 180px" v-if="record.filmName && record.showTime >= new Date()">
              <a @click="showTip(record.id)">校验规则</a>
            </div>
            <div style="width: 180px" v-else>
              <a style="color: rgba(188 189 182)">校验规则</a>
            </div>
          </template>
        </template>
      </a-table>
    </a-card>

    <Tip ref="tifRef"></Tip>
  </page-container>
</template>

<style lang="less" scoped>
.hiddenText {
  display: inline-block;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  line-height: 16px;
}

.formClass {
  :nth-child(1) {
    margin-bottom: 2px;
  }
}

.searchBox {
  margin-bottom: 10px;
  display: flex;

  input {
    width: 200px;
    margin-right: 10px;
  }

  button {
    margin-right: 10px;
  }
}

a:hover {
  color: red;
}

.imgBox {
  width: 50px;
  height: 50px;
  margin: 0 auto;

  img {
    height: 100%;
  }
}

.popoverImg {
  width: 360px;

  img {
    width: 100%;
  }
}
</style>
